package com.dragon.logical.service.website.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.website.IDisplayExampleService;
import com.dragon.repository.entity.DisplayExample;
import com.dragon.repository.mapper.DisplayExampleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 案例展示 服务实现类
 * </p>
 *
 * @author sy
 * @since 2024-03-01
 */
@Service
public class DisplayExampleServiceImpl extends ServiceImpl<DisplayExampleMapper, DisplayExample> implements IDisplayExampleService {

    @Autowired
    DisplayExampleMapper displayExampleMapper;

    @Override
    public List<DisplayExample> exampleList() {
        return displayExampleMapper.selectList(null);
    }

    @Override
    public void displayExampleAdd(DisplayExample displayExample) {
        displayExample.setCreateTime(DateUtil.date());
        displayExampleMapper.insert(displayExample);
    }

    @Override
    public void displayExampleUpdate(DisplayExample displayExample) {
        displayExample.setUpdateTime(DateUtil.date());
        displayExampleMapper.updateById(displayExample);
    }

    @Override
    public void displayExampleDeleted(Integer id) {
        DisplayExample displayExample = displayExampleMapper.selectById(id);
        if (displayExample == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        displayExampleMapper.deleteById(id);
    }
}
